package com.have.blog.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminBlogConstant;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.BlogDto;
import com.have.blog.admin.domain.dto.CommentDto;
import com.have.blog.admin.domain.dto.UserDto;
import com.have.blog.admin.service.AdminBlogService;
import com.have.blog.admin.service.AdminCommentService;
import com.have.blog.admin.service.AdminUserService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.UserCommentMapper;
import com.have.blog.model.models.UserComment;
import com.have.blog.model.models.UserCommentExample;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class AdminCommentServiceImpl implements AdminCommentService {
    @Autowired
    private UserCommentMapper commentMapper;
    @Autowired
    private AdminUserService userService;
    @Autowired
    private AdminBlogService blogService;

    @Override
    public Long getCommentCount(int status) {
        UserCommentExample example = new UserCommentExample();
        example.createCriteria()
                .andStatusEqualTo(status);
        Long count = commentMapper.countByExample(example);
        return count;
    }

    @Override
    public Wrapper deleteBatchComment(List<Integer> uids) {
        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.illegal();
        }
        UserComment userComment = new UserComment();
        userComment.setStatus(BlogStatus.DISABLE.getStatus());
        UserCommentExample example = new UserCommentExample();
        example.createCriteria().andUidIn(uids);
        int update = commentMapper.updateByExampleSelective(userComment, example);
        if (update > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Override
    public Wrapper deleteComment(Integer uid) {
        if (uid == null) {
            return Wrapper.illegal();
        }
        UserComment userComment = commentMapper.selectByPrimaryKey(uid);
        if (userComment == null) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_NOT_EXIST);
        }
        userComment.setStatus(BlogStatus.DISABLE.getStatus());
        int reuslt = commentMapper.updateByPrimaryKeySelective(userComment);
        if (reuslt > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Override
    public Wrapper editComment(CommentDto commentDto) {
        UserComment userComment = commentMapper.selectByPrimaryKey(commentDto.getUid());
        userComment.setContent(commentDto.getContent());
        userComment.setUpdateTime(new Date());
        int update = commentMapper.updateByPrimaryKey(userComment);
        if (update > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Override
    public Wrapper addComment(CommentDto commentDto) {
        UserComment userComment = MyBeanUtils.copyProperties(commentDto, UserComment::new);
        userComment.setStatus(BlogStatus.ENABLE.getStatus());
        int result = commentMapper.insert(userComment);
        if (result > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Override
    public Wrapper<CommonPage<CommentDto>> getPageList(CommentDto commentDto) {
        Page page = PageHelper.startPage(commentDto.getPageNum(), commentDto.getPageSize());
        UserCommentExample example = new UserCommentExample();
        UserCommentExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(commentDto.getKeyword()) && !StringUtils.isEmpty(commentDto.getKeyword().trim())) {
            criteria.andContentLike("%" + commentDto.getKeyword() + "%");
        }
        if (commentDto.getType() != null) {
            criteria.andTypeEqualTo(commentDto.getType());
        }
        if (StringUtils.isNotEmpty(commentDto.getSource()) && !AdminBlogConstant.ALL.equals(commentDto.getSource())) {
            criteria.andSourceEqualTo(commentDto.getSource());
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<UserComment> comments = commentMapper.selectByExample(example);
        List<CommentDto> commentDtos = MyBeanUtils.copyListProperties(comments, CommentDto::new);
        CommonPage<CommentDto> result = CommonPage.restPage(commentDtos);
        result.setTotal(page.getTotal());
        result.setPageNum(page.getPageNum());

        if (!CollectionUtils.isEmpty(commentDtos)) {
            List<Integer> userUids = new ArrayList<>();
            List<Integer> blogUids = new ArrayList<>();
            List<Integer> toUserUid = new ArrayList<>();
            for (CommentDto dto : commentDtos) {
                if (dto.getUserUid() != null) {
                    userUids.add(dto.getUserUid());
                }
                if (dto.getBlogUid() != null) {
                    blogUids.add(dto.getBlogUid());
                }
                if (dto.getToUserUid() != null) {
                    toUserUid.add(dto.getToUserUid());
                }
            }
            Set<Integer> set = new HashSet<>();
            set.addAll(userUids);
            set.addAll(toUserUid);
            List<UserDto> users = userService.getUserByIds(new ArrayList<>(set));
            List<BlogDto> blogByUids = blogService.getBlogByUids(blogUids);
            Map<Integer, UserDto> userMap = new HashMap<>();
            for (UserDto user : users) {
                userMap.put(user.getUid(), user);
            }
            Map<Integer, BlogDto> blogMap = new HashMap<>();
            for (BlogDto blogByUid : blogByUids) {
                blogMap.put(blogByUid.getUid(), blogByUid);
            }
            for (CommentDto dto : commentDtos) {
                if (dto.getUid() != null) {
                    dto.setUser(userMap.get(dto.getUid()));
                }
                if (dto.getToUserUid() != null) {
                    dto.setToUser(userMap.get(dto.getToUserUid()));
                }
                if (dto.getBlogUid() != null) {
                    dto.setBlog(blogMap.get(dto.getBlogUid()));
                }
            }
        }
        return Wrapper.success(result);
    }
}
